package com.jlkf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.jlkf.dao.UserMessageMapper;
import com.jlkf.pojo.UserMessage;
import com.jlkf.service.UserMessageService;
import com.jlkf.service.util.RegexUtil;
import com.platform.server.response.response.pojo.ReturnData;
@Service
public class UserMessageServiceImpl implements UserMessageService{

	@Autowired
	private UserMessageMapper userMessageMapper;
	
	@Override
	public ReturnData getMyMessage(Long uId, Integer pageNo, Integer pageSize) {
		if(RegexUtil.isNull(pageNo)){
			pageNo=1;
		}
		if(RegexUtil.isNull(pageSize)){
			pageSize=10;
		}
		Map<String,Object> map = new HashMap<>();
		map.put("uId", uId);
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<UserMessage> list=userMessageMapper.getUserMessage(map);
			for(UserMessage um:list){
	        	um.setIsRead(1);
	        	userMessageMapper.updateByPrimaryKeySelective(um);
	        }
			return new ReturnData(list,200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"fail");
		}
	}

	//
	@Override
	public Object deleteMeaage(Long id) {
		try {
			int num=userMessageMapper.deleteByPrimaryKey(id);
			if(num==0){
				return new ReturnData(201,"fail");
			}
			return new ReturnData(200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"fail");
		}
	}

}
